package com.ruoyi.web.controller.system;

import com.google.zxing.WriterException;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.QRCodeGenerator;
import com.ruoyi.know.service.ICommunityPostsService;
import com.ruoyi.know.service.ICoursesService;
import com.ruoyi.know.service.IKnowledgePointsService;
import com.ruoyi.know.service.IStatisticsService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * 数据统计
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/index")
public class IndexController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ICoursesService coursesService;
    @Autowired
    private IKnowledgePointsService knowledgePointsService;
    @Autowired
    private ICommunityPostsService communityPostsService;

    @Autowired
    private IStatisticsService statisticsService;

    @GetMapping("/post-trend")
    public AjaxResult postTrend() {
        return AjaxResult.success(statisticsService.getPostTrend());
    }

    @GetMapping("/section-distribution")
    public AjaxResult sectionDistribution() {
        return AjaxResult.success(statisticsService.getSectionDistribution());
    }

    @GetMapping("/user-points-rank")
    public AjaxResult userPointsRank() {
        return AjaxResult.success(statisticsService.getUserPointsRank());
    }

    /**
     * 数据统计
     */
    @GetMapping("/getCount")
    public AjaxResult getCount()
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("userCount", sysUserService.getUserCount());
        ajax.put("courseCount",coursesService.getCourseCount());
        ajax.put("knowledgePointCount", knowledgePointsService.getKnowledgePointCount());
        ajax.put("communityPostsCount",communityPostsService.getCommunityPostsCount());
        return ajax;
    }

    @GetMapping(value = "/qrcode", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] generateQRCode(@RequestParam("frontPort")String frontPort) throws IOException, WriterException {
        String ipv4 = getLocalIPv4Address();
        String url = "http://"+ipv4+":"+frontPort+"/";
        int width = 300;
        int height = 300;
        return QRCodeGenerator.generateQRCodeImage(url, width, height);
    }

    public static String getLocalIPv4Address() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface iface = interfaces.nextElement();
                // 过滤掉回环接口、未启用的接口
                if (iface.isLoopback() || !iface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    if (addr instanceof Inet4Address) {
                        return addr.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
